/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.embed.infra.utils;

import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.cshoo.tattoo.embed.infra.entity.FileEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author 杨翼
 * @since 1.0
 */
public class FileEntityUtils {

    /**
     * 默认换行符，以linux的换行符为标准
     */
    public static String LINE_SEPARATOR = "\n";

    /**
     * 构造文件meta数据
     *
     * @param file 文件
     * @return meta数据
     */
    public static String buildMetadata(MultipartFile file) {
        return "name:" + file.getOriginalFilename() + LINE_SEPARATOR
                + "contentType:" + file.getContentType() + LINE_SEPARATOR
                + "size:" + file.getSize();
    }

    /**
     * 构造file entity对象
     *
     * @param file     文件
     * @param metadata 文件元数据
     * @return 文件对象
     */
    @SneakyThrows
    public static FileEntity buildFileEntity(File file, String metadata) {
        FileEntity fileEntity = new FileEntity();
        fileEntity.setFile(file);
        String[] splitMetaData = StringUtils.split(metadata, LINE_SEPARATOR);
        if (Objects.nonNull(splitMetaData)) {
            for (String metaDataItem : splitMetaData) {
                if (StringUtils.isNotBlank(metaDataItem) && StringUtils.contains(metaDataItem, ":")) {
                    String[] metaDataItemArr = StringUtils.split(metaDataItem, ":");
                    String fieldName = StringUtils.trim(metaDataItemArr[0]);
                    String fieldValue = StringUtils.trim(metaDataItemArr[1]);
                    Field field = FieldUtils.getField(FileEntity.class, fieldName, true);
                    Class<?> fieldType = field.getType();
                    if (fieldType == String.class) {
                        FieldUtils.writeField(field, fileEntity, fieldValue, true);
                    } else if (fieldType == Long.class) {
                        FieldUtils.writeField(field, fileEntity, Long.parseLong(fieldValue), true);
                    }
                }
            }
        }
        return fileEntity;
    }

    /**
     * 获取文件名
     *
     * @param metadata 文件元数据
     * @return 文件名
     */
    public static String getFileName(String metadata) {
        String[] splitMetaData = StringUtils.split(metadata, LINE_SEPARATOR);
        if (Objects.nonNull(splitMetaData)) {
            for (String metaDataItem : splitMetaData) {
                String[] metaDataItemArr = StringUtils.split(metaDataItem, ":");
                String fieldName = StringUtils.trim(metaDataItemArr[0]);
                String fieldValue = StringUtils.trim(metaDataItemArr[1]);
                if (StringUtils.equals(fieldName, "name")) {
                    return fieldValue;
                }
            }
        }
        return null;
    }

    @SneakyThrows
    public static String getFileName(File metadataFile) {
        if(metadataFile.exists()) {
            String metadata = FileUtils.readFileToString(metadataFile, StandardCharsets.UTF_8);
            return getFileName(metadata);
        }
        return null;
    }

}
